﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BookingTour1 : System.Web.UI.Page
{
    eStoreDataContext db = new eStoreDataContext();
    float tong = 0;
    protected void Page_PreRender(object sender, EventArgs e)
    {
        // Lay Id cua tour
        int TourId = int.Parse(RouteData.Values["TourId"].ToString());
        var tour = db.Tours.Single(p => p.TourId == TourId);

        txtTotal.Text = tour.UnitPrice.ToString();

        HiddenField1.Value = tour.UnitPrice.ToString();
        HiddenField2.Value = tour.PriceChidren.ToString();
        HiddenField3.Value = tour.PriceBaby.ToString();
        // Hiển thị thông tin customer
        var user = Session["user"] as Customer;
        if (Session["user"] != null)
        {
            customer_booking.Visible = false;
            txtUsername.Text = user.Fullname;
            txtPhone.Text = user.Phone;
            txtAddress.Text = user.Address;
            txtEmail.Text = user.Email;
        }

    }
    public string mahoa(string pass)
    {
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pass.Trim(), "MD5");
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        int TourId = int.Parse(RouteData.Values["TourId"].ToString());
        var username = Username.Text;
        var pass = txtPassword.Text;
        string passmahoa = mahoa(pass);
        try
        {
            var user = db.Customers
                .Single(c => c.CusId == username && c.Password == passmahoa);
            if (user.Activated == false)
            {
                lblerror.Visible = true;
                lblerror.Text = "Tài khoản của bạn chưa được kích hoạt";
            }
            else
            {
                Session["user"] = user;
                Session["name"] = username;
                Response.Redirect("dat-tour-"+TourId+".html");
            }
        }
        catch
        {
            lblerror.Visible = true;
            lblerror.Text = "Sai thông tin đăng nhập, vui lòng kiểm tra lại!";
            //Response.Redirect("BookingTour.aspx");
        }

    }
    protected void btn_next_Click(object sender, EventArgs e)
    {
        Session["fullname"] = txtUsername.Text;
        Session["phone"] = txtPhone.Text;
        Session["email"] = txtEmail.Text;
        Session["address"] = txtAddress.Text;
        Session["ngaydi"] = txtNgaydi.Text;
        Session["ghichu"] = notes.Text;
        Session["slnl"] = txtNguoiLon.Text;
        Session["slte"] = txtTreEm.Text;
        Session["sltn"] = txtTreNho.Text;
        Session["slpd"] = txtPhongDon.Text;
      //  Session["total"] = txtTotal.Text;

        int slnl =int.Parse(txtNguoiLon.Text);
        int slte = int.Parse(txtTreEm.Text);
        int sltn = int.Parse(txtTreNho.Text);
        int slpd = int.Parse(txtPhongDon.Text);

        int TourId = int.Parse(RouteData.Values["TourId"].ToString());
        var tour = db.Tours.Single(p => p.TourId == TourId);
        // tinh tien
        double tong = 0;
        float giapd = 800000;
        if (slnl > 0)
        {
            tong = slnl * float.Parse(tour.UnitPrice.ToString());
        }
        else { tong = float.Parse(tour.UnitPrice.ToString());
       // Response.Write("<script>alert(" + tong + ")</script>");
        }
        if (slte > 0)
        {
            tong += slte * float.Parse(tour.PriceChidren.ToString()); ;
        }
        if (sltn > 0)
        {
            tong += sltn * float.Parse(tour.PriceBaby.ToString()); ;
        }
        if (slpd > 0)
        {
            tong += slpd * giapd;
        }
        Session["total"] = tong;
       // Response.Write("<script>alert(" + tong + ")</script>");
        Response.Redirect("Confirm.aspx");
    }
  
}